package com.example.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.domain.entity.Category;
import com.example.domain.entity.Dish;
import com.example.domain.entity.Setmeal;
import com.example.exception.RegisterException;
import com.example.mapper.CategoryMapper;
import com.example.mapper.DishMapper;
import com.example.mapper.SetmealMapper;
import com.example.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 菜品及套餐分类 服务实现类
 * </p>
 *
 * @author xiaoduan
 * @since 2024-07-09
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void removeCategory(String ids) {
        List<Dish> dishes = dishMapper.findByCategoryId(ids);
        List<Setmeal> setmeals = setmealMapper.findByCategoryId(ids);
        if (dishes.size() > 0) {
            // return R.error("该分类下有关联的菜品，请先删除关联的菜品");
            throw new RegisterException("该分类下有关联的菜品，请先删除关联的菜品",601);
        }
        if (setmeals.size()>0){
            throw new RegisterException("该分类下有关联的套餐，请先删除关联的套餐",601);
            // return R.error("该分类下有关联的套餐，请先删除关联的套餐");
        }
        categoryMapper.deleteById(ids);
        // return R.success("分类删除成功");
    }

}
